package it.polimi.dei.SeCo.WordNet;

import java.util.ArrayList;
import java.util.List;

import edu.smu.tspell.wordnet.NounSynset;
import edu.smu.tspell.wordnet.Synset;
import edu.smu.tspell.wordnet.SynsetType;
import edu.smu.tspell.wordnet.WordNetDatabase;

/**
 * @author Simone Benefico
 */
public class WordNetTool {
	
	private WordNetDatabase database = null;
	
	/**
	 * Sets the path to the WordNet dictionary locally installed and creates a WordNetDatabase
	 * @param path to to the WordNet dictionary locally installed
	 */
	public WordNetTool(String path) {
		System.setProperty("wordnet.database.dir", path);
		database = WordNetDatabase.getFileInstance(); 
	}
	
	/**
	 * Retrieves the synsets of type <i>type</i> of <i>lemma</i>
	 * @param lemma lemma which the synsets are retrieved for
	 * @param type type of synsets to retrieve
	 * @return array of Synsets
	 */
	public Synset[] getSynsets(String lemma, SynsetType type) {
		return database.getSynsets(lemma, type);
	}
	
	/**
	 * Retrieves the hyponyms of a Synset of type <i>NOUN</i>
	 * @param synsets array of Synset which the hyponyms are retrieved for
	 * @return array of Synset of hyponyms
	 */
	public NounSynset[] getHyponyms(Synset[] synsets) {
		NounSynset nounSynset;
		NounSynset[] hyponyms = null;
		for(int i=0; i<synsets.length; i++) {
			nounSynset = (NounSynset)(synsets[i]);
		    hyponyms = nounSynset.getHyponyms();
		}		
		return hyponyms;
	}
	
	/**
	 * Retrieves the hypernyms of a Synset of type <i>NOUN</i>
	 * @param synsets array of Synset which the hypernyms are retrieved for
	 * @return array of Synset of hypernyms
	 */
	public NounSynset[] getHypernyms(Synset[] synsets) {
		NounSynset nounSynset;
		NounSynset[] hypernyms = null;
		for(int i=0; i<synsets.length; i++) {
			nounSynset = (NounSynset)(synsets[i]);
			hypernyms = nounSynset.getHypernyms();
		}		
		return hypernyms;
	}
	
	/**
	 * Returns the list of Strings from a given array of Synset
	 * @param synsets array of Synset
	 * @return list of Strings from the array of Synset
	 */
	public List<String> synsetsStrings(Synset[] synsets, int count) {
		List<String> synsetsStrings = new ArrayList<String>();
		String[] wordForms = null;
		for (int i = 0; i < count; i++) {
			if(synsets.length>0) {
				wordForms = synsets[i].getWordForms();
				for (int j = 0; j < wordForms.length; j++)
					synsetsStrings.add(wordForms[j]);		
			}
		}
		return synsetsStrings;
	}
	
	/**
	 * Returns the Strings from the first synset of a word
	 * @param synsets array of Synset
	 * @return list of Strings from the first array of Synset
	 */
	public List<String> firstSynsetStrings(Synset[] synsets) {
		return synsetsStrings(synsets, 1);
	}
	
}
